package com.bdqn.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bdqn.entity.Student;
import com.bdqn.util.JdbcUtil;

public class StudentDaoImpl implements StudentDao{

	@Override
	public List<Student> list() {
		//获取连接
		Connection connection = JdbcUtil.getConnection();
		//mysql语句
		String sql="select * from stu";
		//创建一个list集合，将获取的学生信息存到集合中
		List<Student> list=new ArrayList<Student>();
		try {
			//获取sql语句，进行查询
			PreparedStatement statement = connection.prepareStatement(sql);
			ResultSet resultSet = statement.executeQuery();
			//循环遍历查询到的结果，并存到集合中
			while(resultSet.next()) {
				int sid=resultSet.getInt(1);
				String sname=resultSet.getString(2);
				String spwd=resultSet.getString(3);
				String gender=resultSet.getString(4);
				int gid=resultSet.getInt(5);
				Student student = new Student(sid, sname, spwd, gender, gid);
				//将遍历的信息存入集合
				list.add(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

}
